package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Transactional
@Slf4j
@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    //查询
    @Override
    public List<Address> findList() {
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        log.info("用户的id为：{}"+UserHolder.get().getId());
        wrapper.eq(Address::getUserId, UserHolder.get().getId());
        List<Address> addressList = addressMapper.selectList(wrapper);
        return addressList;
    }

    //新增方法
    @Override
    public void save(Address address) {
        address.setUserId(UserHolder.get().getId());
        addressMapper.insert(address);
    }

    //设置默认地址
    @Override
    public void setDefault(Long id) {
        //新方法
        //1. 设置查询条件
        LambdaUpdateWrapper<Address> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Address::getUserId,UserHolder.get().getId());
        //2. 将本用户的默认地址标识设为0
        Address address = new Address();
        address.setIsDefault(0);
        addressMapper.update(address,wrapper);
        //3. 再将本id的默认地址设为1
        Address address1 = new Address();
        address1.setId(id);
        address1.setIsDefault(1);
        addressMapper.updateById(address1);
//        //1.设置查询条件
//        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Address::getUserId,UserHolder.get().getId());
//        wrapper.eq(Address::getIsDefault,1);
//        Integer count = addressMapper.selectCount(wrapper);
//        if (count>1){
//            throw new CustomException("设置了多个默认地址，请联系管理员进行修改");
//        }
//        //2.查询出谁是默认的，并将其默认标识设为0
//        LambdaQueryWrapper<Address> wrapper1 = new LambdaQueryWrapper<>();
//        wrapper1.eq(Address::getUserId,UserHolder.get().getId());
//        List<Address> addressList = addressMapper.selectList(wrapper1);
//        for (Address address : addressList) {
//            if (address.getIsDefault()==1){
//                address.setIsDefault(0);
//                addressMapper.updateById(address);
//            }
//        }
//        //3.将要设置的默认地址标识设为1
//        LambdaQueryWrapper<Address> wrapper2 = new LambdaQueryWrapper<>();
//        wrapper2.eq(Address::getUserId,UserHolder.get().getId());
//        wrapper2.eq(Address::getId,id);
//        Address address = addressMapper.selectOne(wrapper2);
//        //4.如果本身就为1，就给他个提示
//        if (address.getIsDefault()==1){
//            throw new CustomException("必须保留一个默认地址哦");
//        }
//        //5.若不为1
//        address.setIsDefault(1);
//        //address.setUserId(UserHolder.get().getId());
//        addressMapper.updateById(address);

    }

    //查询默认地址
    @Override
    public Address getDefault() {
        //1.设置查询条件
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getUserId,UserHolder.get().getId());
        wrapper.eq(Address::getIsDefault,1);
        //2.根据查询条件进行查询，并返回
        Address address = addressMapper.selectOne(wrapper);
        return address;
    }

    //回显
    @Override
    public Address findById(Long id) {
        Address address = addressMapper.selectById(id);
        return address;
    }

    //更新
    @Override
    public void update(Address address) {
        addressMapper.updateById(address);
    }

    //删除
    @Override
    public void delete(Long[] ids) {
        //不可删除默认地址
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Address::getId,ids);
        wrapper.eq(Address::getUserId,UserHolder.get().getId());
        wrapper.eq(Address::getIsDefault,1);
        int count = addressMapper.selectCount(wrapper);
        if (count==1){
            throw new CustomException("不可删除默认地址");
        }
        addressMapper.deleteBatchIds(Arrays.asList(ids));
    }
}
